﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using MG_WIN.Util;

namespace MG_WIN.DAL
{
    /// <summary>
    /// 数据访问类:MixMatch_detail
    /// </summary>
    public partial class MixMatch_detail
    {
        public MixMatch_detail()
        { }
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            string strsql = "select max(ms_no)+1 from t_mix_match_setting";
            DbHelper db = new DbHelper();
            DbCommand dbCommand = db.GetSqlStringCommand(strsql);
            object obj = db.ExecuteScalar(dbCommand);
            if (obj != null && obj != DBNull.Value)
            {
                return int.Parse(obj.ToString());
            }
            return 1;
        }


        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ms_no)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_mix_match_setting");
            strSql.Append(" where ms_no=" + ms_no + " ");
            DbHelper db = new DbHelper();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(MG_WIN.Model.MixMatch_detail model)
        {
            DbHelper db = new DbHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_mix_match_setting(");
            strSql.Append("ms_no,ms_desc,F135,F136,F137,F144,F138,F145,F143,F139,F147,F148,F111,F112,create_datetime,update_datetime,ms_discnt_link,ms_price_lvl");
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append("" + model.ms_no + ",");
            strSql.Append("'" + model.ms_desc + "',");
            strSql.Append("" + model.priceMultiple + ",");
            strSql.Append("" + model.price + ",");
            strSql.Append("'" + model.startdate + "',");
            strSql.Append("'" + model.starttime + "',");
            strSql.Append("'" + model.enddate + "',");
            strSql.Append("'" + model.endtime + "',");
            strSql.Append("" + model.packageprice + ",");
            strSql.Append("" + model.packagepricemultiple + ",");
            strSql.Append("" + model.limitedqtyamount + ",");
            strSql.Append("" + model.limitedqtyprice + ",");
            strSql.Append("" + model.discountamount + ",");
            strSql.Append("" + model.discountpercent + ",");
            strSql.Append("'" + model.create_datetime + "',");
            strSql.Append("'" + model.update_datetime + "',");
            strSql.Append("" + model.ms_discnt_link + ",");
            strSql.Append("" + model.ms_price_lvl + "");
            strSql.Append(")");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(MG_WIN.Model.MixMatch_detail model)
        {
            DbHelper db = new DbHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_mix_match_setting set ");
            strSql.Append("ms_desc='" + model.ms_desc + "',");
            strSql.Append("F135=" + model.priceMultiple + ",");
            strSql.Append("F136=" + model.price + ",");
            strSql.Append("F137='" + model.startdate + "',");
            strSql.Append("F144='" + model.starttime + "',");
            strSql.Append("F138='" + model.enddate + "',");
            strSql.Append("F145='" + model.endtime + "',");
            strSql.Append("F143=" + model.packageprice + ",");
            strSql.Append("F139=" + model.packagepricemultiple + ",");
            strSql.Append("F147=" + model.limitedqtyamount + ",");
            strSql.Append("F148=" + model.limitedqtyprice + ",");
            strSql.Append("F111=" + model.discountamount + ",");
            strSql.Append("F112=" + model.discountpercent + ",");
            strSql.Append("create_datetime='" + model.create_datetime + "',");
            strSql.Append("update_datetime='" + model.update_datetime + "',");
            strSql.Append("ms_discnt_link=" + model.ms_discnt_link + ",");
            strSql.Append("ms_price_lvl=" + model.ms_price_lvl + "");
            strSql.Append(" where ms_no=" + model.ms_no + " ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ms_no)
        {
            DbHelper db = new DbHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_mix_match_setting ");
            strSql.Append(" where ms_no=" + ms_no + " ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            int rows = db.ExecuteNonQuery(dbCommand);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }		/// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string ms_nolist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_mix_match_setting ");
            strSql.Append(" where ms_no in (" + ms_nolist + ")  ");
            DbHelper db = new DbHelper();
            DbCommand dbcommand = db.GetSqlStringCommand(strSql.ToString());
            int rows = db.ExecuteNonQuery(dbcommand);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public MG_WIN.Model.MixMatch_detail GetModel(int ms_no)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select   ");
            strSql.Append(" ms_no,ms_desc,F135,F136,F137,F144,F138,F145,F143,F139,F147,F148,F111,F112,create_datetime,update_datetime,ms_discnt_link,ms_price_lvl ");
            strSql.Append(" from t_mix_match_setting ");
            strSql.Append(" where ms_no=" + ms_no + " ");
            DbHelper db = new DbHelper();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            MG_WIN.Model.MixMatch_detail model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public MG_WIN.Model.MixMatch_detail DataRowToModel(DataRow row)
        {
            MG_WIN.Model.MixMatch_detail model = new MG_WIN.Model.MixMatch_detail();
            if (row != null)
            {
                if (row["ms_no"] != null && row["ms_no"].ToString() != "")
                {
                    model.ms_no = int.Parse(row["ms_no"].ToString());
                }
                if (row["ms_desc"] != null)
                {
                    model.ms_desc = row["ms_desc"].ToString();
                }
                if (row["F135"] != null && row["F135"].ToString() != "")
                {
                    model.priceMultiple = int.Parse(row["F135"].ToString());
                }
                if (row["F136"] != null && row["F136"].ToString() != "")
                {
                    model.price = double.Parse(row["F136"].ToString());
                }
                if (row["F137"] != null)
                {
                    model.startdate = row["F137"].ToString();
                }
                if (row["F144"] != null)
                {
                    model.starttime = row["F144"].ToString();
                }
                if (row["F138"] != null)
                {
                    model.enddate = row["F138"].ToString();
                }
                if (row["F145"] != null)
                {
                    model.endtime = row["F145"].ToString();
                }
                if (row["F143"] != null && row["F143"].ToString() != "")
                {
                    model.packageprice = double.Parse(row["F143"].ToString());
                }
                if (row["F139"] != null && row["F139"].ToString() != "")
                {
                    model.packagepricemultiple = int.Parse(row["F139"].ToString());
                }
                if (row["F147"] != null && row["F147"].ToString() != "")
                {
                    model.limitedqtyamount = int.Parse(row["F147"].ToString());
                }
                if (row["F148"] != null && row["F148"].ToString() != "")
                {
                    model.limitedqtyprice = double.Parse(row["F148"].ToString());
                }
                if (row["F111"] != null && row["F111"].ToString() != "")
                {
                    model.discountamount = double.Parse(row["F111"].ToString());
                }
                if (row["F112"] != null && row["F112"].ToString() != "")
                {
                    model.discountpercent = double.Parse(row["F112"].ToString());
                }
                if (row["create_datetime"] != null)
                {
                    model.create_datetime = row["create_datetime"].ToString();
                }
                if (row["update_datetime"] != null)
                {
                    model.update_datetime = row["update_datetime"].ToString();
                }
                if (row["ms_discnt_link"] != null && row["ms_discnt_link"].ToString() != "")
                {
                    model.ms_discnt_link = int.Parse(row["ms_discnt_link"].ToString());
                }
                if (row["ms_price_lvl"] != null && row["ms_price_lvl"].ToString() != "")
                {
                    model.ms_price_lvl = int.Parse(row["ms_price_lvl"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ms_no,ms_desc,F135,F136,F137,F144,F138,F145,F143,F139,F147,F148,F111,F112,create_datetime,update_datetime,ms_discnt_link,ms_price_lvl ");
            strSql.Append(" FROM t_mix_match_setting ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            DbHelper db = new DbHelper();
            DbCommand cmd = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(cmd);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ms_no,ms_desc,F135,F136,F137,F144,F138,F145,F143,F139,F147,F148,F111,F112,create_datetime,update_datetime,ms_discnt_link,ms_price_lvl ");
            strSql.Append(" FROM t_mix_match_setting ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            DbHelper db = new DbHelper();
            DbCommand cmd = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(cmd);
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM t_mix_match_setting ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            DbHelper db = new DbHelper();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            object obj = db.ExecuteScalar(dbCommand);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ID desc");
            }
            strSql.Append(")AS Row, T.*  from t_mix_match_setting T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            DbHelper db = new DbHelper();
            DbCommand cmd = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(cmd);
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "t_mix_match_setting");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "System.Collections.Generic.List`1[Maticsoft.CodeHelper.ColumnInfo]");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<MG_WIN.Model.MixMatch_detail> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ms_no,ms_desc,F135,F136,F137,F144,F138,F145,F143,F139,F147,F148,F111,F112,create_datetime,update_datetime,ms_discnt_link,ms_price_lvl ");
            strSql.Append(" FROM t_mix_match_setting ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<MG_WIN.Model.MixMatch_detail> list = new List<MG_WIN.Model.MixMatch_detail>();
            DbHelper db = new DbHelper();
            DbCommand cmd = db.GetSqlStringCommand(strSql.ToString());
            using (IDataReader dataReader = db.ExecuteReader(cmd))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public MG_WIN.Model.MixMatch_detail ReaderBind(IDataReader dataReader)
        {
            MG_WIN.Model.MixMatch_detail model = new MG_WIN.Model.MixMatch_detail();
            object ojb;
            ojb = dataReader["ms_no"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ms_no = (int)ojb;
            }
            model.ms_desc = dataReader["ms_desc"].ToString();
            ojb = dataReader["F135"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.priceMultiple = (int)ojb;
            }
            ojb = dataReader["F136"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.price = (double)ojb;
            }
            model.startdate = dataReader["F137"].ToString();
            model.starttime = dataReader["F144"].ToString();
            model.enddate = dataReader["F138"].ToString();
            model.endtime = dataReader["F145"].ToString();
            ojb = dataReader["F143"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.packageprice = (double)ojb;
            }
            ojb = dataReader["F139"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.packagepricemultiple = (int)ojb;
            }
            ojb = dataReader["F147"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.limitedqtyamount = (int)ojb;
            }
            ojb = dataReader["F148"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.limitedqtyprice = (double)ojb;
            }
            ojb = dataReader["F111"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.discountamount = (double)ojb;
            }
            ojb = dataReader["F112"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.discountpercent = (double)ojb;
            }
            model.create_datetime = dataReader["create_datetime"].ToString();
            model.update_datetime = dataReader["update_datetime"].ToString();
            ojb = dataReader["ms_discnt_link"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ms_discnt_link = (int)ojb;
            }
            ojb = dataReader["ms_price_lvl"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ms_price_lvl = (int)ojb;
            }
            return model;
        }

        #endregion  Method
    }
}